package com.smartens.boit.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

import com.smartens.boit.MainActivity;
import com.smartens.boit.MyApplication2;
import com.smartens.boit.R;
import com.smartens.boit.activity.ControlActivity;
import com.smartens.boit.activity.EquipmentEnergyActivity;
import com.smartens.boit.activity.EquipmentInfoActivity;
import com.smartens.boit.activity.NormalActivity;
import com.smartens.boit.adapter.ManualAdapter;
import com.smartens.boit.adapter.NormalAdapter;
import com.smartens.boit.bean.BaseData;
import com.smartens.boit.bean.ControlBean;
import com.smartens.boit.bean.ElectricBean;
import com.smartens.boit.bean.LoginBean;
import com.smartens.boit.fragment.viewmode.ManualControlViewmode;
import com.smartens.boit.login.LoginActivity;
import com.smartens.boit.util.EditTextUtil;
import com.smartens.boit.util.HttpHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 手动控制
 * @Author songtt
 * @Date 2021/3/4
 */
public class ManualControlFragment extends Fragment {
    private RecyclerView recyclerView;
    private ManualAdapter recycleAdapter;
    private HttpHelper helper;
    private LinearLayoutManager mLinearLayoutManager;
    private CheckBox control;
    ManualControlViewmode viewModel;
    List<ControlBean> mList;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_manual_control, container, false);
        helper = HttpHelper.getInstance();
        recyclerView = view.findViewById(R.id.rv_manual);
        control = view.findViewById(R.id.open_control);
        //  checkAll = view.findViewById(R.id.check_all);
        mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);

        control.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.setIsshoudong(control.isChecked());
            }

        });

        viewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(requireActivity().getApplication())).get(ManualControlViewmode.class);
        mList = viewModel.getList().getValue();
        viewModel.getIsshoudong().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                control.setChecked(aBoolean);
         /*       if (control.isChecked()) {
                    // checkAll.setChecked(true);
                    // 手动控制
                    UpdateOpenModeGataWay("1");
                } else {

                    // 自动控制
                    UpdateOpenModeGataWay("0");
                }*/

                recycleAdapter.setManual(control.isChecked());
                recycleAdapter.notifyDataSetChanged();
            }
        });
        recycleAdapter = new ManualAdapter(mList, this);
        //设置布局管理器
        recyclerView.setLayoutManager(mLinearLayoutManager);
        //设置Adapter
        recyclerView.setAdapter(recycleAdapter);
        initEditTextUtil(view);
        return view;
    }
    private  void initEditTextUtil(View view)
    {
        EditTextUtil editTextUtil=EditTextUtil.getInstance(view,getContext());
        editTextUtil.setKeyboardListener(new EditTextUtil.KeyboardLayoutListener() {
            @Override
            public void onKeyboardStateChanged(boolean isActive, int keyboardHeight) {
                Log.v("editTextUtil",isActive+"------"+keyboardHeight);
            }
        });
    }
    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * 开启自动控制和手动控制
     *
     * @param mode 0：自动模式 1：手动模式
     */
    public void UpdateOpenModeGataWay(String mode) {

        helper.UpdateOpenModeGataWay("32FFD7055643373914721851", mode, new HttpHelper.HttpCallBack<String>() {
            @SuppressLint("LongLogTag")
            @Override
            public void onSuccess(List<String> result) {
                synchronized (ManualControlFragment.class) {
                    Log.i("==UpdateOpenModeGataWay===切换成功", result.get(0));
                }
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onFailure(final String message) {
                synchronized (ManualControlFragment.class) {
                     Log.v("=====UpdateOpenModeGataWay==onFailure", message);
                }
            }
        });
    }

}